<?php

namespace app\admin\controller;

use think\Db;
use app\enumerations\BannerStatusEnum;

class Banner extends Common {

    /**
     * Banner列表
     * @return mixed
     */
    public function index(){

        $requestMap = $this->convertRequestToMap();

        $list = $this->bannerService->getBannerList($requestMap);

        $this->assign('list',$list);
        return $this->fetch();

    }

    /**
     * Banner添加页面
     * @return mixed
     */
    public function add() {

        return $this->fetch();

    }

    /**
     * banner添加操作
     */
    public function addPost() {

        $post = $this->request->post();

        // 轮播图数据
        $bannerData['title'] = $post['title'];
        $bannerData['img'] = $post['img'];
        $bannerData['target_route'] = $post['target_route'];
        $bannerData['status'] = BannerStatusEnum::getEnumValue(BannerStatusEnum::VALID_CODE);

        Db::startTrans();
        try {

            $this->bannerService->saveByData($bannerData);

            Db::commit();

        } catch(\PDOException $e) {

            Db::rollback();

            $this->error('服务器错误,请稍后再试');

        }

        $this->success('添加成功');

    }

    /**
     * Banner编辑页面
     * @return mixed
     */
    public function edit() {

        $id = input("id");

        $info = $this->bannerService->findById($id);

        $this->assign("info", $info);
        return $this->fetch();

    }

    /**
     * banner编辑操作
     */
    public function editPost() {

        $post = $this->request->post();

        $banner = $this->bannerService->findById($post['id']);

        if (isNullOrEmpty($banner)) {
            $this->error('轮播图不存在');
        }

        // 轮播图数据
        $bannerData['title'] = $post['title'];
        $bannerData['img'] = $post['img'];
        $bannerData['target_route'] = $post['target_route'];

        Db::startTrans();
        try {

            $this->bannerService->updateByIdAndData($banner['id'], $bannerData);

            Db::commit();

        } catch(\PDOException $e) {

            Db::rollback();

            $this->error('服务器错误,请稍后再试');

        }

        $this->success('修改成功');

    }


    /**
     * Banner删除
     */
    public function delete() {

        $id = input("id");

        try {

            $result = $this->bannerService->deleteById($id);

            if(false === $result) {
                $this->error($this->bannerService->getError());
            }

            $this->success("Banner删除成功");

        } catch(\PDOException $e) {
            $this->error($e->getMessage());
        }

    }

    /**
     * 上架轮播图操作
     * @return mixed
     */
    public function activate() {

        $id = $this->request->param('id');

        $banner = $this->bannerService->findById($id);

        if($banner['status'] == BannerStatusEnum::getEnumValue(BannerStatusEnum::VALID_CODE)) {
            $this->error("该轮播图已是上架状态");
        }

        Db::startTrans();
        try {

            $this->bannerService->updateByIdAndData($id,
                ["status"=>BannerStatusEnum::getEnumValue(BannerStatusEnum::VALID_CODE)]);

            Db::commit();

        } catch(\PDOException $e) {

            Db::rollback();

            $this->error('服务器错误,请稍后再试');

        }

        $this->success("上架成功");

    }

    /**
     * 下架轮播图操作
     * @return mixed
     */
    public function deactivate() {

        $id = $this->request->param('id');

        $banner = $this->bannerService->findById($id);

        if($banner['status'] == BannerStatusEnum::getEnumValue(BannerStatusEnum::INVALID_CODE)) {
            $this->error("该轮播图已是下架状态");
        }

        Db::startTrans();
        try {

            $this->bannerService->updateByIdAndData($id,
                ["status"=>BannerStatusEnum::getEnumValue(BannerStatusEnum::INVALID_CODE)]);

            Db::commit();

        } catch(\PDOException $e) {

            Db::rollback();

            $this->error('服务器错误,请稍后再试');

        }

        $this->success("下架成功");

    }

}